package com.ctw.mapmycity.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.springframework.context.support.AbstractApplicationContext;

import com.ctw.mapmycity.hbase.controller.PostController;
import com.ctw.mapmycity.model.PostModel;
import com.ctw.mapmycity.model.to.PostConfirmation;
import com.ctw.mapmycity.spring.HBaseContextFactory;

@Path("/post")
public class PostRS{
	
	@Context
	UriInfo uriInfo;
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public PostModel register(PostModel post) {
		
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
			
		// set id
		String id = System.currentTimeMillis() + "";
		
		post.setId(id);
		controller.save(post);
		
		return controller.get(id);
	}
	
	@GET
	@Path ("posts/filter")
	@Produces(MediaType.APPLICATION_JSON)
	public List<PostModel> filterPosts(@QueryParam("opts") String opts) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		
		return controller.find(opts.split(","));
	}
	
	@GET
	@Path ("posts/")
	@Produces(MediaType.APPLICATION_JSON)
	public List<PostModel> get() {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		
		return controller.findAll();
	}
	
	@GET
	@Path ("{ts}")
	@Consumes("text/plain")
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public PostModel get(@PathParam("ts") String ts) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		
		return controller.get(ts);
	}
	
	@POST
	@Path ("delete/{ts}")
	@Consumes("text/plain")
	public String deleteAccount(@PathParam("ts") String ts) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		
		controller.delete(ts);
		
		return "OK";
	}
	
	@POST
	@Path ("action/confirm")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> confirm(PostConfirmation to) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		controller.confirm(to.getPostId(), to.getEmail());
		return controller.getConfirmList(to.getPostId());
	}
	
	@GET
	@Path ("/action/confirm/{postId}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> getConfirmationList(@PathParam("postId") String postId) {
		AbstractApplicationContext context = HBaseContextFactory.getContext();
		PostController controller = context.getBean(PostController.class);
		
		return controller.getConfirmList(postId);
	}
}
